package com.apestech.api.xml.core;

import com.apestech.formwork.xml.core.var.NodeVarExcute;
import com.apestech.framework.db.SqlSession;
import com.apestech.framework.json.JSONArray;
import com.apestech.framework.json.JSONObject;
import com.apestech.framework.util.ExceptionUtil;
import com.apestech.framework.util.ReflectUtil;

import java.util.ArrayList;

/**
 * 执行步骤
 *
 * @author hup
 * @create 2018-04-20-17:35
 */
public class SingleStep {

    private String type;  //与WHILE配套使用，说明执行类型
    private String name;  //结点名称
    private String queryId;  //查询ID
    private JSONObject runParam;  //执行参数
    private SqlSession session;
    private JSONObject input;
    private JSONObject output;
    private String className;
    private String methodName;

    public void execute() throws Exception {
        if(NodeVarExcute.getWHILE().equals(name)){
            if(NodeVarExcute.getSQLWHERE().equals(type)){
                JSONArray list=getList("query", input, this.queryId, className, methodName);


            }
        }
    }

    private JSONArray getList(String type, JSONObject inParam, String id, String className, String methodName) throws Exception {
        JSONArray result=null;
        switch(type){
            case "method":
                Object obj= ReflectUtil.getObject(className);
                if(input==null){
                    result= (JSONArray) ReflectUtil.execMethod(obj, methodName);
                } else {
                    result= (JSONArray) ReflectUtil.execMethod(obj, methodName, inParam);
                }
                break;
            case "query":
                if(this.session==null){
                    ExceptionUtil.exception(this,"execProcess","查询方式执行必需获取session！");
                }
                result=this.session.getForJSON().queryId(id, inParam);
                break;
            case "json":
                //静态类调用
                result=null;
                break;
            case "http":
                //静态类调用
                result=null;
                break;
        }
        return result;
    }

    private JSONObject getSqlWhereParam(JSONArray list){
        JSONObject map=new JSONObject();
        if(list!=null){
            ArrayList<String> klist=output.getKeylist();
            int klen=klist.size();
            int len=list.size();
            for(int i=0; i<len; i++){
                for(int k=0; k<klen; k++){
                    String key=klist.get(k);
                    map.put(key,((JSONObject)list.get(i)).get(key));
                }
            }
        }
        return map;
    }



}
